<?php
/**
 * @文件功能名称  : 菜单管理
 * @Author      : yaoGrace
 * @Date        : 2021/12/16 11:56
 */
class menuController extends adminController{
    public function __init()
    {
        parent::__init();
        $this->menuModel = model("menu");
    }

    public function index()
    {
        //判断是否进入子菜单
        $pid =  isset($this->gets[0])?$this->gets[0]:0;
        //加载菜单表格
        $data['list'] =  $this->menuModel->getlist($pid);
        $data['pid'] = $pid;
        //子菜单
        if($pid  > 0){
            $partent =  $this->menuModel->getlistbyPid($pid);
            $data['backid'] =$partent['pid'];
        }
        $this->data = $data;
    }

    // 添加
    public function add()
    {
        $pid = isset($this->gets[0])?$this->gets[0]:0;
        //进入的是子菜单
        $this->data = $this->menuModel->getlistbyPid($pid);

        // 执行添加操作
        if(PG_POST){
            $data['title']         = trimAll($_POST['title']);
            $data['controller']    = initPOST("controller",'');
            $data['method']        = initPOST('method','');
            $data['ord']           = initPOST("ord",0);
            $data['ishidden']      = initPOST("ishidden",0);
            $data['status']        = initPOST("status",0);
            if($data['title']==""){ $this->json("菜单名不能为空","error"); }
            //判断子菜单编辑时候 控制器和方法名不能为空； 主菜单添加时候可以为空
            $data['pid']           = initPOST("pid",0);
            if($data['pid']>0 && $data['controller']==""){ $this->json("控制器不能为空","error"); }
            if($data['pid']>0 && $data['method']==""){ $this->json("方法名不能为空","error"); }
            $res = $this->menuModel->add($data);
            //返回操作结果
            if($res){
                $this->json("保存成功");
            }else{
                $this->json("保存失败","error");
            }
        }
    }

    public function edit(){
        $mid = isset($this->gets[0])?$this->gets[0]:0;
        $pid = isset($this->gets[1])?$this->gets[1]:0;
        //进入的是子菜单
        $data['parent_menu']= $this->menuModel->getlistbyPid($pid);
        //查询当前具体某一个菜单的数据
        $data['menu'] = $this->menuModel->getlistByMidEqMid($mid);
        //将所有值传递给前台页面
        $this->data = $data;

        // 执行编辑操作
        if(PG_POST){
            //添加和编辑的 公共数据
            $datas['title']         = trimAll($_POST['title']);
            $datas['controller']    = initPOST("controller",'');
            $datas['method']        = initPOST('method','');
            $datas['ord']           = initPOST("ord",0);
            $datas['ishidden']      = initPOST("ishidden",0);
            $datas['status']        = initPOST("status",0);
            //单独编辑时候数据
            $mid                   = initPOST("mid",0);
            $datas['pid']           = initPOST("pid",0);

            if($datas['title']==""){ $this->json("菜单名不能为空","error"); }
            //判断子菜单编辑时候 控制器和方法名不能为空； 主菜单添加时候可以为空
            if($datas['pid']>0 && $datas['controller']==""){ $this->json("控制器不能为空","error"); }
            if($datas['pid']>0 && $datas['method']==""){ $this->json("方法名不能为空","error"); }
            // $this->json($datas,"error");
             $res = $this->menuModel->update($mid,$datas);
            //返回操作结果
            if($res){
                $this->json("保存成功");
            }else{
                $this->json("保存失败","error");
            }
        }
    }


    // 删除菜单
    public function del()
    {
        if(!PG_POST){$this->json("非法操作,code代码：1000","error");}
        $mid = initPOST('mid',0);
        $res = $this->menuModel->getlistByMidEqMid($mid);
        if(!$res){
            // 传递的mid 不存在
            $this->json("非法操作,code代码：1001","error");
        }
        // 传递的mid存在，则操作删除
        $delRes = $this->menuModel->del($mid);
        if($delRes){
            $this->json("删除成功");
        }else{
            $this->json("删除失败","error");
        }
    }

}